﻿using Marmat.Forms.Local;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.Design;

namespace Marmat.Forms.Design
{
    public class DataGridUITypeEditor : System.Drawing.Design.UITypeEditor
    {

        /// <summary>
        /// 获取使用的编辑器样式 System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)方法。
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService edsvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edsvc != null)
            {
                Marmat.Forms.DataGrid dataGrid = context.Instance as Marmat.Forms.DataGrid;
                Marmat.Forms.Local.DataGrid dataGrid2 = context.Instance as Marmat.Forms.Local.DataGrid;
                switch (context.PropertyDescriptor.Name)
                {
                    case nameof(dataGrid2.ConnectionName):
                    case nameof(dataGrid2.LoadTableName):
                        {
                            Marmat.Forms.Design.DatabaseTablesForm frm = new Marmat.Forms.Design.DatabaseTablesForm(dataGrid2);
                            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                            {
                                dataGrid2.ConnectionName = frm.comboBox1.SelectedItem.ToString();
                                dataGrid2.LoadTableName = frm.bDataGrid1.CurrentRow["Name"].ToString();
                                context.OnComponentChanged();
                            }
                        }
                        break;
                    case nameof(dataGrid.Columns):
                        {
                            Marmat.Forms.Design.ColumnCollectionForm frm = new Marmat.Forms.Design.ColumnCollectionForm(dataGrid);
                            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                            {
                                context.OnComponentChanged();
                            }
                        }
                        break;
                    case nameof(dataGrid2.Relation):
                        {
                            Marmat.Forms.RelationSettingForm frm = new RelationSettingForm(dataGrid2);
                            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                            {
                                dataGrid2.Relation.Items.Clear();
                                dataGrid2.Relation.Table = frm.bComboBox1.Value as Marmat.Forms.Local.DataGrid;
                                dataGrid2.Relation.SyncLoadMode = (SyncLoadModeEnum)frm.bComboBox2.Value;
                                foreach (Marmat.Forms.Views.DataGrid.Row row in frm.BDataGrid1.Rows)
                                {
                                    Marmat.Forms.Local.RelationColumn relation = new RelationColumn();
                                    relation.ParentName = row["父表关联列"].ToString();
                                    relation.ChildName = row["子表关联列"].ToString();
                                    dataGrid2.Relation.Items.Add(relation);
                                }
                                context.OnComponentChanged();
                            }
                        }
                        break;
                    case nameof(dataGrid2.SaveProjects):
                        {
                            var frm = new SaveProjectCollentForm(dataGrid2);
                            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                            {
                                dataGrid2.SaveProjects.Clear();
                                dataGrid2.SaveProjects.AddRange(frm.collection);
                                context.OnComponentChanged();
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
            return base.EditValue(context, provider, value);
        }
    }
}
